package permission

import (
	"context"
	"database/sql"
	"fmt"
	"go-admin/base/dao"

	"go-admin/base/internal/svc"
	"go-admin/base/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateLogic) Update(req *types.PermissionUpdateReqDTO) (resp *types.PermissionBooleanRespDTOS, err error) {
	err1 := l.svcCtx.PermissionModel.UpdateNameById(l.ctx, &dao.Permission{
		Name: sql.NullString{
			String: req.Name,
			Valid:  true,
		},
		Number: sql.NullString{
			String: req.Name,
			Valid:  true,
		},
		Id: req.PermissionID,
	})
	if err1 != nil {
		_ = fmt.Errorf(err1.Error())
		return nil, err1
	}

	return &types.PermissionBooleanRespDTOS{
		Data: true,
		Result: types.Result{
			Code: 200,
		},
	}, nil
}
